/**
 * Created by ayu on 2017/6/9.
 */
let rawDataDao = require ('../daos/RawDataDao');
module.exports = {
    saveRawData: function ( data, callBack )
    {
        let [ total = '0', remain = '0', count = '0' ] = [ data.total, data.remain, data.count ];
        //批量插入
        rawDataDao.insertRawDatas (data, function ( done )
        {
            if (done.isSuccess)
            {
                if (parseInt (remain) === 0)
                {
                    //判断 remain 变更上报状态 -> 2 上报成功
                    rawDataDao.updateRawDataStatusBySessionCode ({
                        status      : 2,
                        session_code: done.session_code
                    }, function ( done )
                    {
                        if (done.isSuccess)
                        {
                            callBack ({
                                isSuccess    : true,
                                session_code : done.session_code,
                                upload_status: 2
                            });
                        }
                        else
                        {
                            callBack ({
                                isSuccess   : false,
                                session_code: done.session_code,
                            });
                        }
                    });
                }
                else if (parseInt (remain) > 0)
                {
                    //判断 remain 变更上报状态 -> 1 正在上报
                    rawDataDao.updateRawDataStatusBySessionCode ({
                        status      : 1,
                        session_code: done.session_code
                    }, function ( done )
                    {
                        if (done.isSuccess)
                        {
                            callBack ({
                                isSuccess    : true,
                                session_code : done.session_code,
                                upload_status: 1
                            });
                        }
                        else
                        {
                            callBack ({
                                isSuccess   : false,
                                session_code: done.session_code,
                            });
                        }
                    });
                }
                else
                {
                    //判断 remain 变更上报状态 -> 3 上报失败
                    rawDataDao.updateRawDataStatusBySessionCode ({
                        status      : 3,
                        session_code: done.session_code
                    }, function ( done )
                    {
                        if (done.isSuccess)
                        {
                            callBack ({
                                isSuccess    : true,
                                session_code : done.session_code,
                                upload_status: 3
                            });
                        }
                        else
                        {
                            callBack ({
                                isSuccess   : false,
                                session_code: done.session_code
                            });
                        }
                    });
                }
            }
            else
            {
                callBack ({
                    isSuccess   : false,
                    session_code: done.session_code,
                    msg         : '插入数据库失败'
                });
            }
        });
    }
};